﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetUserEmpListHandler : IRequestHandler<GetUserEmpListCommand, ApiResult<List<UserEmpDto>>>
    {
        private readonly IBaseRepository<UsersModel> _userRepository;
        private readonly IMapper _mapper;

        public GetUserEmpListHandler(IBaseRepository<UsersModel> userRepository, IMapper mapper)
        {
            _userRepository = userRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 根据部门Id获取员工信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<UserEmpDto>>> Handle(GetUserEmpListCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<UserEmpDto>> result = new ApiResult<List<UserEmpDto>>();

            var userEmp = _userRepository.GetAll().Where(p => p.DeparmentId == request.DeparmentId);
            List<UserEmpDto> dotlist = new List<UserEmpDto>();
            foreach(var item in userEmp)
            {
                UserEmpDto emp = new UserEmpDto();
                emp.Id = item.Id;
                emp.label=item.UserName;
                dotlist.Add(emp);
            }
            result.Code = StatusEnum.OK;
            result.Data = dotlist;
            result.Message = "获取成功";
            return Task.FromResult(result);
        }
    }
}
